<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/28
 * Time: 8:50
 */
namespace app\tour\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use app\team\model\Comment as App;
class Comment extends Common
{
    public function comment()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where['a.is_display'] = array('eq',0);
            $where['item_id'] = Session::get('tour_id');
            $where['a.type'] = array('eq',1);
            if (isset($pagedata['nickname']) && $pagedata['nickname'] != '') {
                $where['nickname'] = array('like', '%' . $pagedata['nickname'] . '%');
            }
            if (isset($pagedata['content']) && $pagedata['content'] != '') {
                $where['content'] = array('like', '%' . $pagedata['content'] . '%');
            }

            $count = Db::name('appraise')->alias('a')
                ->field('a.*,u.nickname')
                ->join('user u','a.user_id=u.uid','left')
                ->join('tours g','a.item_id=g.tour_id','left')
                ->where($where)
                ->count();

            $data =  Db::name('appraise')->alias('a')
                ->field('a.*,u.nickname')
                ->join('user u','a.user_id=u.uid','left')
                ->join('tours g','a.item_id=g.tour_id','left')
                ->where($where)
                ->limit($startrow, $pagesize)
                ->order('a.app_id','desc')
                ->select();
            foreach($data as $k=>$v){
                $data[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            }
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));

        } else {
            return view();
        }
    }
}